<template>
	<view>
		
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	export default {
		data() {
			return {
				opid:null,
			};
		},
		created(){
			this.selectOppenId();
		},
		methods:{
			...mapMutations(['openid']),
			selectOppenId() {			
				uni.login({
					success: res => {
						//code值(5分钟失效)
						this.request.selectOppenId({
							code: res.code,
							appid: 'wx23df356f1b08842e',
							secret: '61021a7bd91b1fba54cb7fc3b1a9b893',
							state: 1
						}).then(res => {
							this.opid = res.appOppenId
							this.sesskey = res.sessionKey
							this.sesskeyo(this.sesskey)
					})
					},
				});
			},
			sesskeyo(sesskeys) {
				var that = this;
				wx.getUserInfo({
					success: function(res) {
						let WXBizDataCrypt = require('../pages/Login/js/WXBizDataCrypt.js')
						let appId = 'wx23df356f1b08842e'
						let sessionKey = sesskeys
						let encryptedData = res.encryptedData
						let iv = res.iv
						let pc = new WXBizDataCrypt(appId, sessionKey)
						let data = pc.decryptData(encryptedData, iv)
						let unionid = data.unionId
						let pages = getCurrentPages(); //获取所有页面栈实例列表
						let nowPage = pages[pages.length - 1]; //当前页页面实例
						let prevPage = pages[pages.length - 2]; //上一页页面实例
						uni.navigateBack();
						that.restLists(unionid)
					}
				})
			},
			EmojiExchangeString(str) {
				var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则 
				str = str.replace(patt, function(char) {
					var H, L, code;
					if (char.length === 2) {
						H = char.charCodeAt(0); // 取出高位 
						L = char.charCodeAt(1); // 取出低位 
						code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法 
						return "&#" + code + ";";
					} else {
						return char;
					}
				});
				return str;
			},
			//验证登录信息
			restLists(unionid) {
				this.request.restList({
					id:unionid
				}).then(res => {
					let oppenid = ''
					let id = null
				    oppenid = res.rows
					if (oppenid != '') {
						let oppenids = oppenid.map((item) => {
							return item.appOppenId
						})
						let untids = oppenid.map((item) => {
							return item.id
						})
						oppenids = oppenids.toString()
						uni.setStorage({
							key: 'user',
							data: oppenids
						})
						uni.setStorage({
							key: 'untid',
							data: untids
						})
						this.openid({
							oppenids:oppenids,
							untids:untids
						});
					} else {
						console.log("登录未成功")
						uni.setStorage({
							key: 'user',
							data: ' '
						})
						uni.setStorage({
							key: 'untid',
							data: ' '
						})
					// 	// uni.redirectTo({
					// 	// 	url: '../Login/login'
					// 	// })
					}
				})
			},
		}
	}
</script>

<style>

</style>
